//
//  MGQuoteViewController.m
//  iPad
//
//  Created by 英傑 翁 on 12/7/10.
//  Copyright (c) 2012年 翁英傑. All rights reserved.
//

#import "MGQuoteViewController.h"
#import "MGChartViewController.h"

@implementation MGQuoteViewController

@synthesize chartViewController,delgete_AppDelegate;


#pragma mark -
#pragma mark 載入畫面


- (void)viewDidLoad
{
    NSLog(@"viewDidLoad MGTreeViewController");        
    
    [super viewDidLoad];
    
    //self.clearsSelectionOnViewWillAppear = NO;
    //self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
    
}

-(void)tableview_reload:(NSMutableArray *)cods_a{
    
    if (cods_array !=cods_a) {
        [cods_array release];
        
        cods_array=[cods_a retain];
        [self.tableView reloadData];  
        
    }
    
    
}

// Ensure that the view controller supports rotation and that the split view can therefore show in both portrait and landscape.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
 

- (void)selectFirstRow
{
	if ([self.tableView numberOfSections] > 0 && [self.tableView numberOfRowsInSection:0] > 0) {
		NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
	
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
		[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
	}
}
-(void)selectRow:(int)indix{
    NSLog(@"a");
    if ( [self.tableView numberOfSections] > 0 && [self.tableView numberOfRowsInSection:0] > 0 && [self.tableView numberOfRowsInSection:0] > indix ) {
		NSLog(@"b");
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indix inSection:0];
        
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
		[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
	}
}
#pragma mark -
#pragma mark Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
{
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    
    if (cods_array) {
        return [cods_array count];
    }
    else {
        return 0;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"CellIdentifier";
    
    // Dequeue or create a cell of the appropriate type.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    // Configure the cell.
    if ([cods_array count]> indexPath.row) {
        cell.textLabel.text = [cods_array objectAtIndex:indexPath.row];
    }
    else {
        return nil;
    }
       
    return cell;
}


#pragma mark -
#pragma mark Table view delegate


- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	// When a row is selected, set the detail view controller's detail item to the item associated with the selected row.
    chartViewController.detailItem = [NSString stringWithFormat:@"Row %d", indexPath.row];
    
    NSLog(@"select code=%@",[cods_array objectAtIndex:indexPath.row]);
    
    [delgete_AppDelegate start_draw_chart:[cods_array objectAtIndex:indexPath.row]];
    
}


#pragma mark -
#pragma mark Memory management


- (void)dealloc
{
    
    [cods_array release];    
    
    [chartViewController release];
    [super dealloc];
}


@end
